package com.itheima.mapper;

import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealReport;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface SetmealDao {

    @Insert("insert into t_setmeal values(null,#{name},#{code},#{helpCode},#{sex},#{age},#{price},#{remark},#{attention},#{img})")
    @SelectKey(statement = "select last_insert_id() from dual", before = false,
            resultType = Integer.class, keyColumn = "id", keyProperty = "id"
    )
    int add(Setmeal setmeal);

    @Update("update t_setmeal set name=#{name},code=#{code},helpCode=#{helpCode},sex=#{sex},age=#{age},price=#{price},remark=#{remark},attention=#{attention},img=#{img} where id=#{id}")
    int edit(Setmeal setmeal);

    @Select("select * from t_setmeal where id = #{id}")
    Setmeal findById(String id);


    List<Setmeal> findByCondition(String queryString);

    @Select("SELECT name , count as value from t_setmeal JOIN (\n" +
            "\tSELECT setmeal_id ,count(*) as count from t_order GROUP BY setmeal_id\n" +
            ") AS t ON t_setmeal.id = t.setmeal_id\n")
    List<SetmealReport> getSetmealReport();
}
